﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.IO;
using KMT_Pro.ViewModels.HardwareViewModels;
using KMT_Pro.Utils;

namespace KMT_Pro.Views.HardwareViews
{
    public partial class HardwareDetailInfoView : ChildWindow
    {
        HardwareDetailInfoViewModel viewModel = new HardwareDetailInfoViewModel();

        public HardwareDetailInfoView(string HardwareID, string HardwareName, string HardwareType, string CompanyName, string CompanyTelNum, string ASlimit, string etc, bool isReadOnly)
        {
            InitializeComponent();

            this.DataContext = viewModel;

            var vm = this.DataContext as HardwareDetailInfoViewModel;
            if (vm == null) return;

            vm.HardwareID = HardwareID;
            vm.HardwareName = HardwareName;
            vm.HardwareType = HardwareType;
            vm.CompanyName = CompanyName;
            vm.CompanyTelNum = CompanyTelNum;
            vm.ASlimit = ASlimit;
            vm.Etc = etc;

            if (isReadOnly)
                vm.IsReadOnly = true;
            else
                vm.IsReadOnly = false;
        }

        private void ChildWindow_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            var vm = this.DataContext as HardwareDetailInfoViewModel;
            if (vm == null) return;
            vm.editClick.Execute(null);
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void addImgButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog saveDialog = new OpenFileDialog();

            saveDialog.Filter = "JPEG Images|*.jpg|PNG Images|*.png";

            if (saveDialog.ShowDialog().GetValueOrDefault(false))
            {
                //open the file as a stream
                if (saveDialog.File != null && saveDialog.File.Name != "")
                {
                    BitmapImage bitImage = new BitmapImage();
                    bitImage.SetSource(saveDialog.File.OpenRead());

                    //Image1 is object of Image 
                    Image1.Source = bitImage;
                    Image1.Stretch = Stretch.Fill;

                    //check the file size. It shoud be less than 1MB
                    if (saveDialog.File.Length < 1048576)
                    {
                        Stream stream = saveDialog.File.OpenRead();
                        //put the stream in the BinaryReader
                        BinaryReader binary = new BinaryReader(stream);
                        //Read bytes from the BinaryReader and put them into a byte array.
                        Byte[] file = binary.ReadBytes((int)stream.Length);
                        var vm = this.DataContext as HardwareDetailInfoViewModel;
                        if (vm == null) return;

                        vm.addImg(vm.HardwareID, file);
                    }
                }
            }
        }
    }
}

